package cn.tedu._05mvcboot03.common.exception;

import cn.tedu._05mvcboot03.common.response.JsonResult;
import cn.tedu._05mvcboot03.common.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * Validation验证框架异常:声明参数方式数据校验
     */
//    @ExceptionHandler
//    public JsonResult doHandle(ConstraintViolationException ex){
//        String message = ex.getMessage().split(":")[1].trim();
//        log.error("ConstraintViolationException = " + message);
//
//        return new JsonResult(StatusCode.VALIDATED_ERROR, message);
//    }

    @ExceptionHandler
   public JsonResult ccoException(MethodArgumentNotValidException ex){
        String message= ex.getFieldError().getDefaultMessage();
        log.error("ccoException="+message);
        return new JsonResult(StatusCode.VALIDATED_ERROR,message);
    }

    @ExceptionHandler
    public JsonResult Exception(ConstraintViolationException ex){
        String message=ex.getMessage();
        log.error("Exception="+message);
        return new JsonResult(StatusCode.VALIDATED_ERROR,message);
    }

}
